program write_tp_2km_config

  use mongof_common_mod

  implicit none

  character(30) mongo_uri
  character(30) mongo_db
  character(30) mongo_col
  integer cid

  integer, parameter :: nlev = 19
  real(8), parameter :: lev(nlev) = [                       &
    2.0d0, 30.0d0, 50.0d0, 70.0d0, 80.0d0, 90.0d0, 100.0d0, &
    110.0d0, 120.0d0, 130.0d0, 140.0d0, 150.0d0, 160.0d0,   &
    170.0d0, 180.0d0, 200.0d0, 220.0d0, 250.0d0, 300.0d0    &
  ]

  integer, parameter :: nband_wdir = 16
  real(8), parameter :: band_wdir(nband_wdir) = [                    &
    0d0, 22.5d0, 45d0, 67.5d0, 90d0, 112.5d0, 135d0, 157.5d0, 180d0, &
    202.5d0, 225.0d0, 247.5d0, 270.0d0, 292.5d0, 315.0d0, 337.5d0    &
  ]

  integer, parameter :: nband_wsp = 16
  real(8), parameter :: band_wsp(nband_wsp) = [                     &
    1.5d0, 3.5d0, 4.5d0, 5.5d0, 6.5d0, 7.5d0, 8.5d0, 9.5d0, 10.5d0, &
    11.5d0, 12.5d0, 13.5d0, 14.5d0, 15.5d0, 16.5d0, 17.5d0          &
  ]

  interface
    integer(c_int) function write_zlev(cid, nlev, z) result(res) bind(c, name='write_zlev')
      use iso_c_binding
      integer(c_int), intent(in), value :: cid
      integer(c_int), intent(in), value :: nlev
      real(c_double), intent(in) :: z(nlev)
    end function write_zlev
    integer(c_int) function write_band_wdir(cid, nband_wdir, band_wdir) result(res) bind(c, name='write_band_wdir')
      use iso_c_binding
      integer(c_int), intent(in), value :: cid
      integer(c_int), intent(in), value :: nband_wdir
      real(c_double), intent(in) :: band_wdir(nband_wdir)
    end function write_band_wdir
    integer(c_int) function write_band_wsp(cid, nband_wsp, band_wsp) result(res) bind(c, name='write_band_wsp')
      use iso_c_binding
      integer(c_int), intent(in), value :: cid
      integer(c_int), intent(in), value :: nband_wsp
      real(c_double), intent(in) :: band_wsp(nband_wsp)
    end function write_band_wsp
  end interface

  call get_command_argument(1, mongo_uri)

  mongo_uri = 'mongodb://' // trim(mongo_uri) // char(0)
  mongo_db = 'hntp' // char(0)
  mongo_col = 'tp_2km_config'
  call handle_mongo_error(mongoc_connect(mongo_uri, mongo_db, mongo_col, cid), __FILE__, __LINE__)

  call handle_mongo_error(write_zlev(cid, nlev, lev), __FILE__, __LINE__)

  call handle_mongo_error(write_band_wdir(cid, nband_wdir, band_wdir), __FILE__, __LINE__)

  call handle_mongo_error(write_band_wsp(cid, nband_wsp, band_wsp), __FILE__, __LINE__)

  call mongoc_final()

end program write_tp_2km_config
